const App = getApp();

Page({
	data: {
		logged: !1,
		title: App.Config.title
	},
	onLoad(option) {
		if (option){
			this.setData({
				globalOption: option
			})
		}
	},
	onShow() {

	},
	login() {
		wx.getUserProfile({
			desc:'获取用户相关信息',
			success: res => {
				App.globalData.userInfo = res.userInfo
				console.log(App.globalData.userInfo);

				//调用数据
				let param = {
					mname:App.globalData.userInfo.nickName,
					addr:App.globalData.userInfo.avatarUrl
				};
				param =App.Tools.extend(param,this.data.globalOption);
				//调用数据
				App.HttpService.saveData(param,"/member_Add").then(data=>{
					console.log(data);
					wx.setStorageSync("mid",data[0].jieguo);
					wx.setStorageSync("pic",App.globalData.userInfo.avatarUrl)
					wx.setStorageSync("nc",App.globalData.userInfo.nickName)
				});

				wx.switchTab({
					url:"/pages/index/index"
				})
			}
		})
	},
	goIndex() {

	},
	showModal() {
		App.WxService.showModal({
			title: '友情提示',
			content: '获取用户登录状态失败，请重新登录',
			showCancel: !1,
		})
	},
	wechatDecryptData(cb) {
		var thiz= this;
		let code;
		App.WxService.login()
			.then(data => {
				console.log('wechatDecryptData', data.code)
				code = data.code
				return App.WxService.getUserInfo()
			})
			.then(data => {
				return App.HttpService.wechatDecryptData({
					dashboardId:'37816',
					encryptedData: data.encryptedData,
					iv: data.iv,
					rawData: data.rawData,
					signature: data.signature,
					code: code,
					loadmsg:'登录中',
					debug: App.Config.debug?"1":"0"
				})
			})
			.then(data => {
				if (data.code == 0) {
					App.Session.setUser(data)
					cb();
				} else{
					thiz.showModal()
				}
			})
	},
	wechatSignIn(cb) {
		if (App.Session.getOpenId()) return
		App.WxService.login()
			.then(data => {
				console.log('wechatSignIn', data.code)
				return App.HttpService.wechatSignIn({
					code: data.code
				})
			})
			.then(data => {
				console.log('wechatSignIn', data)
				if (data.meta.code == 0) {
					App.WxService.setStorageSync('openId', data.data.openId)
					cb()
				} else if(data.meta.code == 40029) {
					App.showModal()
				} else {
					App.wechatSignUp(cb)
				}
			})
	},
	wechatSignUp(cb) {
		App.WxService.login()
			.then(data => {
				console.log('wechatSignUp', data.code)
				return App.HttpService.wechatSignUp({
					code: data.code
				})
			})
			.then(data => {
				console.log('wechatSignUp', data)
				if (data.meta.code == 0) {
					App.WxService.setStorageSync('openId', data.data.openId)
					cb()
				} else if(data.meta.code == 40029) {
					App.showModal()
				}
			})
	},
	signIn(cb) {
		if (App.Session.getOpenId()) return
		App.HttpService.signIn({
			username: 'admin',
			password: '123456',
		})
			.then(data => {
				console.log(data)
				if (data.meta.code == 0) {
					App.WxService.setStorageSync('openId', data.data.openId)
					cb()
				}
			})
	},
})